# -*- coding:UTF-8 -*-
from flask import Flask,request, url_for,render_template,redirect,jsonify, session
from bs4 import BeautifulSoup
import time, requests, re, chardet, urllib, ssl, random,time,os,shutil,subprocess

app = Flask(__name__)
app.secret_key = os.urandom(24)


@app.route('/')
def hello_world():
	# phpcode()
	return redirect(url_for('vlogin'))
	return jsonify(id=123,name="张三",year=18)
@app.route('/index.html')
def indexpage():
	return 'index page'
@app.route('/user/<userid>')                     #单个参数
def user(userid):
 	return '用户id是' + userid
@app.route('/km/<kmid>/kw/<kwid>')               #多个参数
def muchfun(kmid, kwid):
	return '多个变量的传参: km:'+kmid + 'kw:'+ kwid

@app.route('/redict')
def tiaozhaun():                                 #函数之间的跳转
	print('五秒以后跳转登陆页')
	time.sleep(5)
	return redirect(url_for('indexpage'))

@app.route('/login', methods = ['GET', 'POST'])
def login():
	if request.method == 'POST':
		return do_the_login()

	else:
		return show_the_login_form()



def do_the_login():
	return '123'
def show_the_login_form():
	return '456'

@app.route('/parent')                          # 模版继承
def parentfun():
	url = status = request.args.get('url')
	historylog = filelist()
	#获取当前目录
	cwd = os.getcwd()
	return render_template('child.html', title="下载", url= url, filearray = historylog, cwd = cwd)
@app.route('/compony', methods = ['GET', 'POST'])
def compony():
	status = 'fail'
	if request.method == 'POST':
	# 	if 'url' in request.form:
		if request.form['url'] != '':
			status = download(request.form['url'])
	if status != 'fail':
		return redirect(url_for('parentfun', url = status))
	if certifical() == False:
		return redirect(url_for('vlogin'))
	return render_template('compony.html', title = "首页")



#####################以上为练习#############################
#
#登陆页
#
@app.route('/login.html')
def log():
	status = request.args.get('status')          #获取url中的参数
	return render_template('log.html', error = status)


#
#登陆页验证
#
@app.route('/vlogin', methods = ['GET', 'POST'])
def vlogin():
	if request.method == 'POST':
		if vdentify(request):
			return redirect(url_for('compony'))
		else:
			return redirect(url_for('log', status = False))
	else:
		return redirect(url_for('log'))

#
#验证账号和密码
#
def vdentify(data):
	print(data.form)
	if data.form['username'] == 'admin' and data.form['password'] == '111111':
		setsession(data.form['username'])
		return True
	else:
		return False


'''
404
'''
@app.route('/no.html')
def nofundpage():
	return render_template('page_not_found.html')


'''
下载历史
'''
def filelist():
	#列出当前目录的所有文件和文件夹
	name = os.listdir()
	#获取当前目录
	url = os.getcwd()
	print(url)
	# exit()
	newname = []
	for item in name:
		if os.path.isdir(item):
			if item != 'static' and item != 'templates' and item != '.git' and item != '__pycache__':
				newname.append(item)
	print(name)
	return newname
	# exit()






#只需要用上python的bytes和str两种类型转换的函数encode()、decode()即可
def download(url):
	header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
	w = urllib.request.Request(url, headers = header)
	data1 = urllib.request.urlopen(w).read()
	chardit1 = chardet.detect(data1)
	coding = chardit1['encoding']
	print(chardit1['encoding'])
	# return coding


	# url = 'http://www.huatu.com'
	res = requests.get(url, headers = header, verify = False)
	if coding == 'GB2312':
		data = res.content.decode('gbk')
	else:
		data = res.content.decode(coding)
	# data = res.text
	# ff = guolv(data)
	# print(ff)
	# exit()
	#创建文件夹
	newdir = mkdirfile()
	#下载图片
	downimg(res.text, newdir)
	#去除所有的html标签
	dr = re.compile(r'<[^>]+>',re.S)
	dd = dr.sub('\n',data)
	print(dd)
	# exit()
	#下载文本
	date = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
	with open(date+".txt","wb") as code:
		code.write(dd.encode())
	movefile(date+".txt", newdir)
	return newdir
def guolv(htmlstr):
	# htmlstr = re.sub('<script type="text/javascript">.*</script>','',htmlstr)
	re_cdata=re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #匹配CDATA
	re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
	re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
	re_br=re.compile('<br\s*?/?>')#处理换行
	re_h=re.compile('</?\w+[^>]*>')#HTML标签
	re_comment=re.compile('<!--[^>]*-->')#HTML注释
	s=re_cdata.sub('',htmlstr)#去掉CDATA
	s=re_script.sub('',s) #去掉SCRIPT
	s=re_style.sub('',s)#去掉style
	s=re_br.sub('\n',s)#将br转换为换行
	s=re_h.sub('',s) #去掉HTML 标签
	s=re_comment.sub('',s)#去掉HTML注释
	return s

def downimg(data, newdir):
	#img
	dt = BeautifulSoup(data)
	dt2 = dt.find_all('img')
	print(dt2)
	p = 0
	for x in dt2:
		#获取所有的属性
		attrall = x.attrs
		if 'src' in attrall:
			src = attrall['src']
		else:
			if 'data-src' in attrall:
				src = attrall['data-src']
				# print(src)
				# exit()
			else:
				continue
		result = src.find('http://')
		if result == -1:
			result = src.find('https://')
			if result == -1:
				continue
		if result != -1:

			try:
				urllib.request.urlretrieve(src,'%s.jpg' % p)
			except ValueError:
				pass
			movefile(str(p)+'.jpg', newdir)
			p+=1

		# print(x.attrs['src'])
	# print(dt2)
	# exit()
#创建文件夹
def mkdirfile():
	date = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
	#获取当前目录
	dirlist = os.getcwd()
	newdir = dirlist + '/' + date
	os.mkdir(newdir,0o755)
	return newdir
#移动文件到文件夹
def movefile(name,path_url):
	shutil.move(name,path_url)

#执行php程序打包下载
def phpcode():
	proc = subprocess.Popen("php ./putout.php", shell=True, stdout=subprocess.PIPE)
	script_response = proc.stdout.read()
	print(script_response)
#存储session
def setsession(name):
	session['username'] = name
#退出
@app.route('/logout')
def logout():
	print(session['username'])
	print(session.pop('username'))
	print(session.get('username') == None)
	if session.get('username') == None:
		return redirect(url_for('vlogin'))
	else:
		return redirect(url_for('compony'))
#判断是否登陆
def certifical():
	print('cr12133')
	print(session.get('username'))
	if session.get('username') == None:
		return False
	else:
		return True



if __name__ == '__main__':
	ssl._create_default_https_context = ssl._create_unverified_context
	app.debug = True
	app.run(port=5210)










